﻿using System;
using System.Data;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Web.UI;
using Dcms.Common;
using Dcms.BLL;
using Dcms.Model;

public partial class Admin_UserManage_UserList : System.Web.UI.Page
{
    private static readonly string CODER = ConfigurationManager.AppSettings["Coder"];
    public CompetenceLib CL = new CompetenceLib();
    Dcms_AdminBLL aBll = new Dcms_AdminBLL();
    Dcms_AdminModel aModel = new Dcms_AdminModel();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {          
            CL.IsLogin();

            if (!IsPostBack)
            {
                string strDesignRoleID = ConfigurationManager.AppSettings["DesignRoleID"];
                this.hidAmount.Value = aBll.GetDataCount(("select count(*) from Dcms_Admin where Admin_RoleId <> " + strDesignRoleID).ToString());
                this.hidSql.Value = "select * from Dcms_Admin where Admin_RoleId <> " + strDesignRoleID;
                ReBinder(1);
            }
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Admin_UserManage_UserList.Page_Load", ex);
            throw ex;
        }
    }

    protected void BindRpt(object sender, PagingEventArgs e)
    {
        this.ReBinder(e.pageIndex);
    }

    private void ReBinder(int PageIndex)
    {
        this.repAdmin.DataSource = aBll.GetDataListByPage(this.hidSql.Value, PageIndex, 11);
        this.repAdmin.DataBind();
        pagingctrl.InitPc(Convert.ToInt32(this.hidAmount.Value), PageIndex, 10);
    }

    protected void repAdmin_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            //隔行变色
            if (e.Item.ItemIndex % 2 == 1)
            {
                ((HtmlTableRow)e.Item.FindControl("tr1")).Style.Add("background", "#F2F2F2");
            }

            Literal lit = (Literal)e.Item.FindControl("litRole");

            ArrayList CoderAl = aBll.GetCoders();
            if (!CoderAl.Contains(DataBinder.Eval(e.Item.DataItem, "Admin_Name").ToString()))
            {
                ((LinkButton)e.Item.FindControl("HypDelete")).Attributes.Add
                    ("onclick", "return confirm('确定要删除该记录吗？');");
            }
            else {
                ((LinkButton)e.Item.FindControl("HypDelete")).Attributes.Add
                    ("onclick", "alert('系统默认管理员不能删除！');return false;");
            }((HyperLink)e.Item.FindControl("hypEdit")).NavigateUrl = String.Format
                            ("UserInfo.aspx?AdmID=" + ((HiddenField)e.Item.FindControl("hidID")).Value);
        }
    }

    protected void repAdmin_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            ArrayList CoderAl = aBll.GetCoders();
            if (!CoderAl.Contains(aBll.GetModelByID(Convert.ToInt32(((HiddenField)e.Item.FindControl("hidID")).Value)).Admin_Name))
            {
                if (e.CommandName == "delete")
                {
                    aBll.DeleteModelByID(Convert.ToInt32(((HiddenField)e.Item.FindControl("hidID")).Value));

                    Session.Add("Message", "删除用户成功，请返回！");
                    Session.Add("Url", "UserManage/UserList.aspx?id=" + strPermissionID);
                    Response.Redirect("../Message.aspx",false);
                }
            }
        }
    }
    public String getRoleName(String RoleID,String Name) {
        ArrayList CoderAl = aBll.GetCoders();
        if (!CoderAl.Contains(Name))
        {
            if (Convert.ToInt32(RoleID) == 0)
            {
                return "未设置";
            }
            else
            {
                Dcms_RoleBLL rBll = new Dcms_RoleBLL();
                Dcms_RoleModel rModel = rBll.GetModelByID(Convert.ToInt32(RoleID));
                if (rModel.Role_Id == 0)
                {
                    return "未设置";
                }
                else
                {
                    return rModel.Role_Name;
                }
            }
        }
        else {
            return "系统默认管理员";
        }
    }

    protected string strPermissionID
    {
        get
        {
            return Request.QueryString["id"] + "";
        }
    }
}
